package cn.fastoo.web.backstage.web.controller.system;

import cn.fastoo.common.dao.system.AdminSystemOrganizationDao;
import cn.fastoo.common.dao.system.PermissionDao;
import cn.fastoo.common.entity.system.AdminSystemOrganization;
import cn.fastoo.common.entity.system.Permission;
import cn.fastoo.common.sampleframe.dto.JsonResult;
import cn.fastoo.common.sampleframe.search.QuerySearch;
import cn.fastoo.common.sampleframe.search.Search;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * 用户页面 控制器.
 */
@Controller
@RequestMapping("/system/permission")
public class AdminSystemPermissionController {
	private static Logger logger = LoggerFactory.getLogger(AdminSystemPermissionController.class);

	@Autowired
	private PermissionDao dao;

	@RequestMapping("权限管理")
	public String 权限管理() {
		return "/system/权限管理";
	}

	@RequestMapping
	public String index() {
		return "/system/permission/index";
	}

	@RequestMapping("/manager")
	@ResponseBody
	public String manager(@ModelAttribute Permission newData, QuerySearch querySearch, String op) {
		Search search = querySearch.getSearch();
		search.addSort(new Sort(Sort.Direction.ASC,"id"));
		if(org.apache.commons.lang.StringUtils.isNotBlank(op)){
			if("add".equals(op)){
				dao.insert(newData);
				//logger.info(loginUser, SystemLog.TYPE_AUTH_ROLE_ADD,String.format("用户角色[%s]",authRole.getRoleName()));
			}else {
				Permission oldData = dao.findById(newData.getId());
				if(oldData==null){
					return JsonResult.success("错误参数,请检查");
				}
				if("save".equals(op)){
					dao.update(newData);
				}else if("del".equals(op)){
					dao.delete(oldData);
				}
			}
			return JsonResult.success("操作成功");
		}
		return JsonResult.querySearchSuccess(dao.find(search),querySearch);
	}



}
